 <%@ page language="java" contentType="image/png"
    pageEncoding="UTF-8" import="java.util.*, java.net.*, java.io.* "%>
<%
	//this jsp simply passes a url along to the appropriate geoserver instance and returns
	//the results back to the client as an attachment
	
    final String asterik = "%2a";
	final String openParen = "%28";
	final String closeParen = "%29";
	final String openBracket = "%5B";
	final String closeBracket = "%5D";
	final String colon = "%3a";
	final String tilde = "%7e";
	final String comma = "%2c";
	final String space = "%20";
		
    String serverLocation = request.getParameter("server");
    String layerNames = request.getParameter("layers");
    String srs = request.getParameter("srs");
    String bbox = request.getParameter("bbox");
    String height = request.getParameter("height");
    String width = request.getParameter("width");
    String format = request.getParameter("format");
    String type = request.getParameter("type");
    String sld = request.getParameter("sld");

    String urlFormat = URLEncoder.encode(format, "UTF-8");
    //switch based on format to add dpi settings, change/add header info
    String url$;
	Boolean isImage = false;
	url$ = serverLocation + "?service=wms&version=1.1.1&request=GetMap&format=" + urlFormat + "&SRS=" + srs;
    url$ += "&styles=&bbox=" + bbox + "&layers=" + layerNames;
	if (format.equals("image/png")){
		isImage = true;
		/*int requestedDpi = 90;//dpi & size options?
		url$ += "&format_options=dpi:" + Integer.toString(requestedDpi) + ";";
		width = Integer.toString(Math.round(Integer.parseInt(width) * requestedDpi / 90));
		height = Integer.toString(Math.round(Integer.parseInt(height) * requestedDpi / 90));
		*/
		url$ += "&tiled=false&transparent=true";
	} 
	url$ += "&height=" + height + "&width=" + width;
	
	if ((sld != null)&&(!sld.equals("null"))){
		url$ += "&sld_body=" + URLEncoder.encode(sld, "UTF-8");
	}
	
	System.out.println(" in WMSGetMapProxy, url = " + url$);
	URL url = new URL(url$);
	HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
	InputStream inputStream = httpConnection.getInputStream();
	// forward the response from the server to the browser
	OutputStream outputStream = response.getOutputStream();
	//for images, set the content disposition
	if (isImage){
		response.setHeader("Content-disposition","attachment; filename=\"" + layerNames + ".png\"");
	} else {
		response.setHeader("Content-disposition","attachment; filename=\"" + layerNames + ".kml\"");
	}
	//response.setContentType(httpConnection.getContentType());
	System.out.println("Download starting...");

	InputStream bufferedIn = new BufferedInputStream(inputStream);
		try {
			int currentBytes;
			while ((currentBytes = bufferedIn.read()) != -1) {
				//System.out.println("Receiving " + currentBytes + " bytes");
				outputStream.write(currentBytes);
			} 
		} finally {
		
			try {
				bufferedIn.close();
			}
			finally {
				outputStream.close();
			}
		}

		System.out.println("Download complete for " + layerNames);

    	//needs to be a servlet, not a jsp

%>
